package com.zry.study.glide;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.util.Base64;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


/**
 * Created by 19428 on 2017/6/30.
 */

public class SmallPhoto {

    public void smallPhoto(String path) {


        //图片进行压缩，并保存在SDCard
        try {
            Bitmap bm = PictureUtil.getSmallBitmap(path);
            FileOutputStream fos = new FileOutputStream(new File(path));
            bm.compress(Bitmap.CompressFormat.JPEG, 80, fos);
        } catch (Exception e) {
            Log.e("TAG", "error", e);
        }
    }


    //获取指定文件大小
    public long getFileSize(String path) throws Exception {

        File file = new File(path);

        if (file == null) {
            return 0;
        }
        long size = 0;
        if (file.exists()) {
            FileInputStream fis = null;
            fis = new FileInputStream(file);
            size = fis.available();
        }
        return size;
    }


    //将图片转成Bitmap

    public Bitmap getPhotoToBitmap(String filePath) throws Exception {
        //判断图片的大小。如果图片超过10M，对图片进行压缩
        if (getFileSize(filePath) > 10485760) {
            return BitmapFactory.decodeFile(filePath, getBitmapOption(2)); //将图片的长和宽缩小味原来的1/2
        } else {
            return BitmapFactory.decodeFile(filePath, getBitmapOption(1)); //将图片的长和宽缩小味原来的1/2
        }
    }


    private BitmapFactory.Options getBitmapOption(int inSampleSize) {
        System.gc();
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPurgeable = true;
        options.inSampleSize = inSampleSize;
        return options;
    }


    //将图片转成字符串
    public String Bitmap2StrByBase64(Bitmap bit) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        if (getBitmapsize(bit) > 5240000) {
            bit.compress(Bitmap.CompressFormat.JPEG, 50, bos);//参数100表示不压缩
        } else {
            bit.compress(Bitmap.CompressFormat.JPEG, 50, bos);//参数100表示不压缩
        }
        byte[] bytes = bos.toByteArray();
        return Base64.encodeToString(bytes, Base64.DEFAULT);
    }


    public String Bitmap2StrByBase64(String filePath) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Bitmap bm = PictureUtil.getSmallBitmap(filePath);
        //将bitmap一字节流输出 Bitmap.CompressFormat.PNG 压缩格式，100：压缩率，baos：字节流
        bm.compress(Bitmap.CompressFormat.PNG, 20, baos);
        baos.close();
        byte[] buffer = baos.toByteArray();
        Log.e("TAG", "图片的大小：" + buffer.length);
        //将图片的字节流数据加密成base64字符输出
        String photo = Base64.encodeToString(buffer, 0, buffer.length, Base64.DEFAULT);
        Log.e("TAG", photo);

        return photo;
    }


    //获取bitmap的大小
    public long getBitmapsize(Bitmap bitmap) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
            return bitmap.getByteCount();
        }
        // Pre HC-MR1
        return bitmap.getRowBytes() * bitmap.getHeight();
    }

    /**
     * 根据文件路径，获取文件名称
     * @param path
     * @return
     * @throws Exception
     */
    public String getImgName(String path) throws Exception {
        // 判空操作必须要有 , 处理方式不唯一 , 根据实际情况可选其一 。
        if(path == null) {
            throw new Exception("路径不能为null"); // 处理方法一
        }

        int start=path.lastIndexOf("/");
        int end=path.lastIndexOf(".");
        if(start!=-1 && end!=-1){
            return path.substring(start+1,end);//包含头不包含尾 , 故:头 + 1
        }else{
            return "";
        }
    }

}
